package com.tuyue.police.weixin.controller.page;

import com.tuyue.police.biz.IUserBiz;
import com.tuyue.police.pojo.User;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author: 王金海
 * @Description: 用户
 * @Date: Created by Administrator on 2018/3/23.
 * @Modified By:
 */
@Controller
@RequestMapping("/page/user")
@Slf4j
public class PageController {

    @Autowired
    private WxMpService wxService;
    @Autowired
    private IUserBiz userBiz;

    /**
     * 授权
     */
    @RequestMapping("authorizeUrl")
    public String authorizeUrl(String url) {
        String url1 = wxService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
        log.error(url1);
        return "redirect:" + url1;
    }

    /**
     * 进入绑定页面
     *
     * @param code
     * @param state
     * @param model
     * @return
     * @throws WxErrorException
     */
    @RequestMapping("bangding")
    public ModelAndView bangding(String code, String state, Model model) throws WxErrorException {
//        log.error(code + "++++++++++++" + state);
//        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxService.oauth2getAccessToken(code);
//        String openId = wxMpOAuth2AccessToken.getOpenId();
//        log.error("openId+++++++++++++" + openId);
//        model.addAttribute("openId", openId);
        model.addAttribute("openId", 111);
        System.out.println("进入了");
        return new ModelAndView("bdzc", "user", model);
    }

    /**
     * 个人中心
     *
     * @param code
     * @param model
     * @return
     * @throws WxErrorException
     */
    @RequestMapping("grzl")
    public ModelAndView grzl(String code, Model model) throws WxErrorException {
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxService.oauth2getAccessToken(code);
        String openId = wxMpOAuth2AccessToken.getOpenId();
        log.error("openId+++++++++++++" + openId);
        User user = userBiz.findByopenId(openId);
//        User user = userBiz.findByopenId("obZjo0QAPvwrg0YP1EdN0J2oa51Q");
        if (ObjectUtils.isEmpty(user)) {
            log.error("进入了注册页" + openId);
            model.addAttribute("openId", openId);
            return new ModelAndView("bdzc", "user", model);
        } else {
            log.error("进入了个人中心页" + openId);
            user.setUserPassword("");
            model.addAttribute("userName", user.getUserName());
            model.addAttribute("creatTime", user.getCreatTime());
            return new ModelAndView("grzx", "user", model);
        }


    }

    /**
     * 注册页面
     *
     * @param openId
     * @param model
     * @return
     * @throws WxErrorException
     */
    @GetMapping("zc")
    public ModelAndView zc(String openId, Model model) throws WxErrorException {
        log.error("注册页面传参"+openId);
        model.addAttribute("openId", openId);
        return new ModelAndView("zc", "user", model);
    }
    @PostMapping("zc")
    public ModelAndView zcform(String openId, Model model) throws WxErrorException {
        log.error("注册页面接收参数"+openId);
        // TODO: 2018/3/26  
        return new ModelAndView("zc", "user", model);
    }

}
